package com.zx.idc.backend.gui.slog;


import com.baomidou.mybatisplus.plugins.Page;
import com.zx.idc.backend.gui.api.management.basicdata.query.IdcBasicDataQueryService;
import com.zx.idc.backend.gui.common.BasicBaseApi;
import com.zx.idc.backend.gui.common.Result;
import com.zx.idc.backend.gui.slog.service.SlogAbnormalIpInterfaceService;
import com.zx.idc.common.exception.EncryptException;
import com.zx.idc.ds.slog.entity.DealData;
import com.zx.idc.ds.slog.entity.ReportData;
import com.zx.idc.ds.slog.entity.SlogAbnormalIp;
import com.zx.idc.ds.slog.service.ISlogAbnormalIpService;
import com.zx.idc.ds.slog.vo.SlogAbnormalIpVo;
import com.zx.idc.internal.exception.SendMessageException;
import com.zx.idc.xml.entity.idcinfomanage.IdcInfoManage;
import com.zx.idc.xml.entity.idcinfomanage.commandinfo.CommandInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 异常IP 前端控制器
 * </p>
 *
 * @author rxliuli
 * @since 2019-01-31
 */
@RestController
@RequestMapping("/api/slogAbnormalIp")
public class SlogAbnormalIpController extends BasicBaseApi<ISlogAbnormalIpService, SlogAbnormalIp> {

    @Autowired
    private SlogAbnormalIpInterfaceService slogAbnormalIpReportService;
    @Autowired
    private IdcBasicDataQueryService idcBasicDataQueryService;


    @GetMapping("/query-page")
    @RequiresPermissions("regulatory-log:slog-abnormal-ip")
    public Result<Page<SlogAbnormalIpVo>> page(Page<SlogAbnormalIpVo> page, SlogAbnormalIpVo slogAbnormalIpVo) {
        return success(service.pageByParam(page, slogAbnormalIpVo));
    }

    @GetMapping("/list")
    @RequiresPermissions("regulatory-log:slog-abnormal-ip")
    public Result<List<SlogAbnormalIpVo>> list(SlogAbnormalIpVo slogAbnormalIpVo) {
        return success(service.listByParam(slogAbnormalIpVo));
    }

    @PostMapping(value = "/report-data")
    @RequiresPermissions("regulatory-log:slog-abnormal-ip")
    public Result<Boolean> reportData(@RequestBody ReportData reportData) {
        return success(slogAbnormalIpReportService.reportData(reportData));
    }

    /**
     * 手动上报监测到的异常数据
     * @param abnormalIpList
     * @return
     */
    @PostMapping(value = "/reportAbnormalData")
    @RequiresPermissions("regulatory-log:slog-abnormal-ip")
    public Result<Boolean> reportAbnormalData(@RequestBody List<SlogAbnormalIpVo> abnormalIpList){
        idcBasicDataQueryService.uploadAbnormalIpForManual(abnormalIpList);
        return success(true);
    }

    /**
     * 异常IP处置
     *
     * @param dealData
     * @return
     */
    @PostMapping(value = "/handle")
    @RequiresPermissions("regulatory-log:slog-abnormal-ip:edit")
    public Result<Boolean> handle(@RequestBody DealData dealData) {
        try {
            return success(service.updateAndHandle(dealData));
        } catch (SendMessageException e) {
            log.error("异常IP处置异常", e);
        } catch (EncryptException e) {
            log.error("异常IP处置加密异常", e);
        }
        return success(false);
    }

    /**
     * 异常IP取消处置
     *
     * @param dealData
     * @return
     */
    @PostMapping(value = "/cancel-handle")
    @RequiresPermissions("regulatory-log:slog-abnormal-ip:edit")
    public Result<Boolean> cancelHandle(@RequestBody DealData dealData) {
        try {
            return success(service.updateAndCancelHandle(dealData));
        } catch (SendMessageException e) {
            log.error("异常IP取消处置异常", e);
        } catch (EncryptException e) {
            log.error("异常IP取消处置加密异常", e);
        }
        return success(false);
    }
}

